package _10netty;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @description:
 * @author: wang zhen
 * @create: 2022-08-05 16:06
 */
public class BIOclient {

    @Test
    public void client() {

        Socket socket = null;
        OutputStream outputStream = null;
        InputStream inputStream = null;
        try {
            //1.创建客户端对象，并指定连接的IP地址或主机名+端口号
            socket = new Socket("127.0.0.1", 8000);

            //2.获取这个通道的流，输出数据到通道中
            outputStream = socket.getOutputStream();

            //3.发送数据
            outputStream.write("你好".getBytes());
            socket.shutdownOutput();//通过shutdownOutput高速服务器已经发送完数据，后续只能接受数据


        } catch (Exception e) {
            e.printStackTrace();

        } finally {

            //5.关闭资源
            try {
                if (inputStream != null) {
                    inputStream.close();
                }

                if (outputStream != null) {
                    outputStream.close();
                }

                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
